import { Snowflake } from "api/structures/utils/Snowflake"
import { handleClientReady } from "components/GuildChannelPage"

interface GuildModel {
    id: Snowflake
    name: string
    icon?: string
}

interface GuildListModel {
    id: string
    channelName: string
    userAvatar: string
    itemEnabled: boolean
}

declare const listModel: Qml.ListModel<GuildModel>;
declare const guildChannelListModel: Qml.ListModel<GuildListModel>;

declare const listChannel1View: Qml.ListView;
declare const listChannel2View: Qml.ListView;

function handleReady() {

    console.log("GuildListPanel.handleReady")

    window.client.on("ready", () => {

        listModel.clear()

        const guilds = Object.keys(window.client.guilds).map(id => window.client.guilds[id]);
        guilds.sort((a, b) => (a.position || 0) - (b.position || 0)).forEach(guild => {
            listModel.append({
                id: guild.id,
                name: guild.name,
                icon: guild.iconURL
            })
        })
    })

    window.client.on("disconnect", () => {
        listModel.clear()
    })
}

function handleDestroy() {

    console.log("GuildListPanel.handleDestroy")

}

function openPage(type: string, id: number) {
    window.client.emit("debug", "Open page " + type + " " + id)


    listChannel1View.visible = type==="dm"
    listChannel2View.visible = type!=="dm"

    // window.client.emit("debug", "listView " + window.initialPage)

    if (type!=="dm") {

        window.client.emit("debug", "guildChannelListModel " + guildChannelListModel)

        
        //@ts-ignore
        console.log("handleClientReady.0=" + window.handleClientReady)

        //@ts-ignore
        console.log("handleClientReady.1=" + handleClientReady)

        handleClientReady(guildChannelListModel, id+"")
    }
}